<?php
require_once('../essential.php');

$view = "user";

if(isset($_GET['function']))
{
	$function = $_GET['function'];

	if(isset($_GET['id']))
	{
		$function($_GET['id']);	
	}else{
		$function();	
	}
}
function index()
{
	global $view;
	
	header("location:../view/".$view."/index.php");
}

function form()
{
	global $view;
	
	if(isset($_GET['id']))
	{
		header("location:../view/".$view."/form.php?user=".$_GET['id']);
	}else{
		header("location:../view/".$view."/form.php");
	}
}

function filter()
{
	global $view;
	if(!empty($_POST['group']) || ($_POST['status'] != "") || !empty($_POST['name']))
	{
		$filter = Array();
		
		if(!empty($_POST['group'])){array_push($filter, " id_group=".mysql_real_escape_string($_POST['group']));}
		if($_POST['status'] != ""){array_push($filter, " sys_user.active =".mysql_real_escape_string($_POST['status']));}
		if(!empty($_POST['name'])){array_push($filter, " sys_user.name LIKE '%".mysql_real_escape_string($_POST['name'])."%'");}
		
		
		$filter_string = "?filter=WHERE".implode(' AND', $filter);
		header("location:../view/".$view."/list.php".$filter_string);
	}else{
		header("location:../view/".$view."/list.php");
	}
}


function insert()
{

	global $view;
	$user = new User();
	$user->set(
		$_POST['name'],
		$_POST['login'],
		$_POST['pass'],
		(isset($_POST['active']) ? 1 : 0),
		date("Ymd"),
		$_POST['group']
	);
	$user->insert();
	
	header("location:../view/".$view."/index.php");
}

function update()
{
	global $view;
	$user = new User();
	$user->set(
		$_POST['name'],
		$_POST['login'],
		$_POST['pass'],
		(isset($_POST['active']) ? 1 : 0),
		$_POST['dt_register'],
		$_POST['group'],
		$_POST['id']
	);
	
	$user->update();
	
	header("location:../view/".$view."/form.php?user=".$_POST['id']."&&message=Registro atualizado com sucesso");
}

function delete()
{
	global $view;
	
	$id = $_GET['id'];
	
	$user = new User();
	
	$user->delete($id);
	
	header("location:../view/".$view."/index.php");
	
	
}

function Error()
{
	header("location:../../error.php");
}